<template>
  <div :class="{ hidden: hidden }" class="pagination-container">
    <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :background="background"
      :layout="layout"
      :page-sizes="pageSizes"
      :total="total"
      v-bind="$attrs"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script lang="ts" setup>
import { type PropType } from 'vue';
import { scrollTo } from '@/utils/scroll-to';

defineOptions({
  name: 'RePagination'
});

const currentPage = defineModel('page', { default: 1 });
const pageSize = defineModel('limit', { default: 10 });
const $props = defineProps({
  total: {
    required: true,
    type: Number
  },
  pageSizes: {
    type: Array as PropType<Array<number>>,
    default() {
      return [10, 20, 30, 50];
    }
  },
  layout: {
    type: String,
    default: 'total, sizes, prev, pager, next, jumper'
  },
  background: {
    type: Boolean,
    default: true
  },
  autoScroll: {
    type: Boolean,
    default: true
  },
  hidden: {
    type: Boolean,
    default: false
  }
});
const $emit = defineEmits(['pagination']);

const handleSizeChange = val => {
  $emit('pagination', { page: currentPage, limit: val });
  if ($props.autoScroll) {
    scrollTo(0, 800);
  }
};
const handleCurrentChange = val => {
  $emit('pagination', { page: val, limit: pageSize });
  if ($props.autoScroll) {
    scrollTo(0, 800);
  }
};
</script>

<style scoped>
.pagination-container.hidden {
  display: none;
}
</style>
